package best.driver.android.Utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {

    /** yyyy-MM-dd HH:mm:ss */
    public final static SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /** yyyy.MM.dd HH:mm */
    public final static SimpleDateFormat sDateFormatForPromotion = new SimpleDateFormat("yyyy.MM.dd HH:mm");

    /** yyyy-MM-dd HH:mm */
    public final static SimpleDateFormat YMD_HM = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public final static SimpleDateFormat YMD_HM2 = new SimpleDateFormat("yy/MM/dd HH:mm");

    /** yyyy-MM-dd */

    public final static SimpleDateFormat sDateFormatYMD = new SimpleDateFormat("yyyy-MM-dd");

    public final static SimpleDateFormat sDateFormatMD = new SimpleDateFormat("M月d日 HH:mm");
    
    public final static SimpleDateFormat sDateFormatHM = new SimpleDateFormat("HH:mm");

    public final static SimpleDateFormat sDateFormatHMS = new SimpleDateFormat("HH:mm:ss");

    public final static SimpleDateFormat sDateFormatYM = new SimpleDateFormat("yyyy年MM月");


    public static String getMyCallPhone(String time) {
        long ms = 0;
        try {
            ms = Long.parseLong(time);
        } catch (NumberFormatException e) {
            ms = System.currentTimeMillis();
        }
        return parseTime(YMD_HM, ms);
    }

    public static String convertToYMD(String time) {
        return TimeUtil.sDateFormatYMD.format(getTime(TimeUtil.sDateFormat, time));
    }

    public static String convertToYMD(long time) {
        return TimeUtil.sDateFormatYMD.format(time);
    }

    public static String convertToHM(String time) {
        return TimeUtil.sDateFormatHM.format(getTime(TimeUtil.sDateFormatHMS, time));
    }

    
    public static String convertToHM2(String time) {
    	return TimeUtil.sDateFormatHM.format(getTime(TimeUtil.sDateFormat, time));
    }

    public static String convertToHM2(long time) {
        return TimeUtil.sDateFormatHM.format(time);
    }

    public static String convertToYM(long time) {
        return TimeUtil.sDateFormatYM.format(time);
    }

    public static String convertToYMDHM(String time) {
        return TimeUtil.YMD_HM.format(getTime(TimeUtil.sDateFormat, time));
    }

    public static String convertToYMDHM(long time) {
        return TimeUtil.YMD_HM.format(time);
    }

    public static String convertToMDHM(String time) {
        return TimeUtil.sDateFormatMD.format(getTime(TimeUtil.sDateFormat, time));
    }

    public static String convertToMDHM(long time) {
        return TimeUtil.sDateFormatMD.format(time);
    }

   public static String formatTime(String pscheduledTime) {
        try {
            String dateStr = TimeUtil.convertToYMD(pscheduledTime);
            Date date = TimeUtil.parseTimeFromString(dateStr, TimeUtil.sDateFormatYMD);

            String nowStr = TimeUtil.getStringWithFormat(new Date(),"yyyy-MM-dd");
            Date now = TimeUtil.parseTimeFromString(nowStr, TimeUtil.sDateFormatYMD);
            long timeLong =(now.getTime() - date.getTime())/(24*60*60*1000);
            if (timeLong == 0) {
                return "今天 " + TimeUtil.convertToHM2(pscheduledTime);
            }
            if (timeLong == 1) {
                return "昨天 " + TimeUtil.convertToHM2(pscheduledTime);
            }
            if (timeLong == -1) {
                return "明天 " + TimeUtil.convertToHM2(pscheduledTime);
            }
        } catch (Exception e) {
            return TimeUtil.convertToYMDHM(pscheduledTime);
        }
        return TimeUtil.convertToYMDHM(pscheduledTime);
    }

    public static String formatTime(long pscheduledTime) {
        try {

            long currTime = System.currentTimeMillis();
            Calendar mCalendar = Calendar.getInstance();
            mCalendar.setTime(new Date(currTime));

            int year = mCalendar.get(Calendar.YEAR);
            int month = mCalendar.get(Calendar.MONTH);
            int day = mCalendar.get(Calendar.DAY_OF_MONTH);

            mCalendar.set(year, month, day, 0, 0, 0);
            long minToday = mCalendar.getTimeInMillis();
            if (pscheduledTime + 60000 - minToday < 0) {
                return TimeUtil.convertToMDHM(pscheduledTime);
            }

            long timeLong =(pscheduledTime + 60000 - minToday)/(24*60*60*1000);
            //要改的话用day判断而不是秒，打开再改
            if (timeLong == 0) {
                return "今天 " + TimeUtil.convertToHM2(pscheduledTime);
            }
            if (timeLong == 1) {
                return "明天 " + TimeUtil.convertToHM2(pscheduledTime);
            }
//            if (timeLong == -1) {
//                return "昨天 " + TimeUtil.convertToHM2(pscheduledTime);
//            }
        } catch (Exception e) {
            return TimeUtil.convertToMDHM(pscheduledTime);
        }
        return TimeUtil.convertToMDHM(pscheduledTime);
    }

    /** 获取系统当前时间(时间形式) **/
    public static String getcurrentTimes() {
        return parseTime(sDateFormat, System.currentTimeMillis());
    }

    /********************************************
     * 解析服务器返回的时间戳
     * 
     * @return 年.月.日 时:分
     */
    public static String parserTimeYMDHM(long time) {
        return parseTime(sDateFormatForPromotion, time);
    }

    /********************************************
     * 解析服务器返回的时间戳
     * 
     * @return 年/月/日 时-分
     */
    public static String parserTimeYMDHM2(String time) {
        return parseTime(YMD_HM2, Long.valueOf(time));
    }

    public static String parserTimeYMDHM3(Long time) {
        return parseTime(YMD_HM2, time);
    }


    /********************************************
     * 解析服务器返回的时间戳
     * 
     * @return 月-日 时-分
     */
    public static String parserTime(String time) {
        return parseTime(sDateFormatMD, Long.valueOf(time));
    }

    /********************************************
     * 解析服务器返回的时间戳
     * 
     * @return 年-月-日 时-分-秒
     */
    public static String parsetDetailTime(String time) {
        return parseTime(sDateFormat, Long.valueOf(time));
    }

    /** 时间（long） **/
    public static long getTime(SimpleDateFormat format, String strDate) {
        if (TextUtils.isEmpty(strDate)) {
            return 0;
        }
        try {
            Date date = format.parse(strDate);
            long time = date.getTime();
            return time;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    
    public static String getTime(SimpleDateFormat format, long time) {
        return format.format(new Date(time));
	}
    
    public static String getTime(String format, long time) {
        return new SimpleDateFormat(format).format(new Date(time));
    }

    public static boolean isExpried(long savedExprieTime) {
        if (savedExprieTime == 0) {
            return true;
        }
        long curTime = getTime(sDateFormatYMD, getcurrentTimes());
        return savedExprieTime < curTime;
    }

    public static boolean durating(String startAt, String endAt) {
        long start = getTime(sDateFormatYMD, startAt);
        long end = getTime(sDateFormatYMD, endAt);
        long cur = getTime(sDateFormatYMD, parseTime(sDateFormatYMD, System.currentTimeMillis()));
        return cur >= start && cur <= end;
    }

    public static String parseTime(SimpleDateFormat format, long time) {
        return format.format(new Date(time));
    }

    /**************************
     * 转化时间格式
     * 
     * @param orderTime
     *            订单时间
     * @param from_now_on
     *            多久
     * @return 转化成 年/月/日 时：分
     */
    public static String getDate(String orderTime, String from_now_on) {
        int ordertime = Integer.valueOf(orderTime);
        int fromnowon = Integer.valueOf(from_now_on);
        Long timestamp = Long.parseLong(String.valueOf(ordertime + fromnowon)) * 1000;
        String date = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(new Date(timestamp));
        return date;
    }

    @SuppressLint("SimpleDateFormat")
    public static Date parseTimeFromString(String dateTime, String format) {
        SimpleDateFormat formatDate = new SimpleDateFormat(format);
        try {
            return formatDate.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    @SuppressLint("SimpleDateFormat")
    public static Date parseTimeFromString(String dateTime, SimpleDateFormat format) {
        try {
            return format.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    @SuppressLint("SimpleDateFormat")
    public static String getStringWithFormat(Date dateTime, String format) {
        SimpleDateFormat f = new SimpleDateFormat(format);
        return f.format(dateTime);
    }

    public static long getTimestamp(Date date1) throws ParseException {
        Date date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("1970/01/01 08:00:00");
        long l = date1.getTime() - date2.getTime() > 0 ? date1.getTime()- date2.getTime() : date2.getTime() - date1.getTime();
        return l;
    }
    
    /**
     * 获取当前日期是星期几<br>
     * 
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String convertToDoubleDigit(int num) {
        if(num < 10) {
            return "0" + num;
        }
        return String.valueOf(num);
    }

    public static String millToPayWaitingMinute(String mill){
        int mi = Integer.parseInt(mill);
        if(mi < 0){
            return "1";
        }
        return  (mi+59)/60 +"";
    }

    public static String convertMillisTime(long millisSeconds) {
        return convertSecondsTime(millisSeconds / 1000);
    }

    public static String convertSecondsTime(long seconds) {
        int minute = (int) (seconds / 60);
        int hour = minute / 60;
        int second = (int) (seconds % 60);
        minute %= 60;
        return String.format("%02d:%02d:%02d", hour, minute, second);
    }

}
